package org.jeecg.modules.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.app.entity.AppClient;
import org.jeecg.modules.app.entity.AppFeedback;
import org.jeecg.modules.app.entity.AppUpgrade;
import org.jeecg.modules.app.enums.AppPlatformTypeEnums;
import org.jeecg.modules.app.mapper.AppClientMapper;
import org.jeecg.modules.app.mapper.AppFeedbackMapper;
import org.jeecg.modules.app.mapper.AppUpgradeMapper;
import org.jeecg.modules.app.req.FeedbackReq;
import org.jeecg.modules.app.service.IAppFeedbackService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartRequest;

/**
 * @Description: 问题反馈
 * @Author: sean
 * @Date:   2024-09-28
 * @Version: V1.0
 */
@RequiredArgsConstructor
@Service
public class AppFeedbackServiceImpl extends ServiceImpl<AppFeedbackMapper, AppFeedback> implements IAppFeedbackService {

    @Value(value="${jeecg.uploadType}")
    private String uploadType;

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    private final AppUpgradeMapper appUpgradeMapper;

    private final AppClientMapper appClientMapper;

    @Override
    public IPage<AppFeedback> selectJoinPage(Page page, AppFeedback params) {
        return baseMapper.selectJoinPage(page, params);
    }

    @Override
    public void advice(FeedbackReq feedbackReq, MultipartRequest files) {
        StringBuffer savePath = new StringBuffer();
        files.getMultiFileMap().forEach( (name, multipartFiles)->{
            String filePath = CommonUtils.uploadnew(multipartFiles.get(0), uploadpath, "feedback", uploadType);
            savePath.append(filePath);
            savePath.append(";");
        });

        AppClient appClient = appClientMapper
                .selectOne(new QueryWrapper<AppClient>().eq("code", feedbackReq.getAppCode()));
        if(ObjectUtil.isNotEmpty(appClient) ){
            AppPlatformTypeEnums appPlatformTypeEnums = AppPlatformTypeEnums.getEnumByKey(feedbackReq.getPlatformType());
            AppUpgrade appUpgrade = appUpgradeMapper.selectLatestByClientIdAndPlatformType(appClient.getId(), appPlatformTypeEnums.getValue());
            AppFeedback appFeedback = new AppFeedback();
            appFeedback.setUpgradeId(appUpgrade.getId());
//        appFeedback.setAppId(appUpgrade.getAppId());
//        appFeedback.setAppName(appClient.getAppName());
            appFeedback.setVersionName(appUpgrade.getVersionName());
            appFeedback.setEmail(feedbackReq.getEmail());
            appFeedback.setProblem(feedbackReq.getProblem());
            appFeedback.setAttachment(savePath.toString());
            appFeedback.setStatus("0");
            appFeedback.setSysOrgCode(appUpgrade.getSysOrgCode());
            baseMapper.insert(appFeedback);
        }
    }
}
